package highscore;

import java.util.Vector;

/**
 * contains all the data in the highscore.
 * 
 * @author Duncan Swedman Skomakar'n
 */
public class Highscore
{
    //Rows is a vector that contains all the rows in the Highscore.
    //All instances must be of type HighscoreRow.

    /**
     * Vector to save all the rows in the Highscore in. Each entry  has its own row.
     */
    private Vector<HighscoreRow> rows;

    /**
     * Highscore constructor.
     */
    public Highscore()
    {
		this (new Vector<HighscoreRow>());
    }

    /**
     * Highscore constructor.
     *
     * @param rows Use an existing vector to store the rows.
     */
    public Highscore(Vector<HighscoreRow> rows)
    {
		this.rows = rows;
    }

    /**
     * Adds an HighscoreRow to the 'rows' vector.
     *
     * @param row The row to add to the Highscore
     */
    public void addItem(HighscoreRow row)
    {
		rows.addElement(row);
    }

    /**
     * Removes an item from the Highscore.
     *
     * @param index The index to remove.
     */
    public void removeItem(int index)
    {
		rows.remove(index);
    }

    /**
     * Inserts the item at the specified position in the list.
     *
     * @param item The item to add.
     * @param index Where to add it.
     */
    public void insertItemAt(HighscoreRow item, int index)
    {
		rows.insertElementAt(item, index);
    }

    /**
     * Sets the row at a certain index.
     *
     * @param index The index to manipulate.
     * @param row The row to add.
     */
    public void setRowAt(int index,HighscoreRow row)
    {
        //Check if the specified index is within bounds.
		if ((index < 0) || (index > rows.size()))
            rows.set(index,row);
    }

    /**
     * Returns the row at the specified position. If there does not exist
     * anything at the specified position, a null-type object will be returned.
     *
     * @param index The index of the row to return.
     * @return The row at the specified index.
     */
    public HighscoreRow getRowAt(int index)
    {
		//Check if the specified index is out of bounds, and if so, return null.
		if ((index < 0) || (index > rows.size()))
			return null;

		//The range is valid. Return the HighscoreRow at the specified index.
		return rows.get(index);
    }

    /**
     * Returns the entire vector of rows.
     *
     * @return The vector.
     */
    public Vector<HighscoreRow> getAllRows()
    {
        return rows;
    }

    /**
     * Adds the specified row to the highscore and places it above all lower
     * scores. If the new row.score >= an old row, it is placed above that one.
     *
     * @param row The orw to add and place.
     */
    public void addItemByScore(HighscoreRow row)
    {
		for (int i = 0; i < rows.size(); i++)
		{
			if (row.score >= getRowAt(i).score)
			{
			insertItemAt(row, i);
			return;
			}
		}
		addItem(row);
    }

    /**
     * Set the highscore rows in this highscore to the same one as another highscore
     * IMPORTANT - This will not make it another highscore, only its content
     * Referens may work as normal
     * @param h The highscore to set the rows to
     */
    public void setHighscore(Highscore h)
    {
		rows = h.getAllRows();
    }

    /**
     * Returns the number of rows in the highscore.
     *
     * @return The row-count.
     */
    public int getRowCount()
    {
		return rows.size();
    }

    /**
     * Generated by NetBeans - Returns a hashcode for the object
     * @return a hashcode for the object
     */
    @Override
    public int hashCode()
    {
		int hash = 3;
		hash = 43 * hash + (this.rows != null ? this.rows.hashCode() : 0);
		return hash;
    }

    @Override
    public boolean equals(Object obj)
    {
		if (this.getClass() != obj.getClass())
			return false;

		Highscore hs = (Highscore) obj;

		if (hs.getRowCount() != this.getRowCount())
			return false;

		for (int i = 0; i < this.getRowCount(); i++)
		{
			if (!hs.getRowAt(i).equals(this.getRowAt(i)))
			return false;
		}
		return true;
    }
}